package BinarySearch;

import java.lang.annotation.Target;

public class BinarySearchNoRecur {
    public static void main(String[] args) {
        int[] arr = {1,3, 8, 10, 11, 67, 100};
        int target = 100;
        int mid = binsearch(arr,0,arr.length-1,target);
//        int mid = binarySearchcsdn(arr, target);
        System.out.println("target = "+ target +"在第"+ mid +"位" );
    }


    /**
     *
     * @param arr 待查找的数组，arr是升序排序
     * @param target 需要查找的数
     * @return 返回对应下表 -1 没有找到
     */
    public static int binarySearch(int[] arr, int target) {
        int left = 0;
        int right = arr.length - 1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if(arr[mid] == target){
                return mid;
            }else if(arr[mid] > target){
                right = mid - 1;
            }else {
                left = mid + 1;
            }

        }

        return -1;

    }

    public static int binsearch(int arr[], int left, int right, int target){

        int mid = (left + right) / 2;
        if(arr[mid] == target){
            return mid;
        }
        if(left > right){
            return -1;
        }else if(arr[mid]> target){
            return binsearch(arr,left,mid - 1,target);

        }else if(arr[mid] < target){
            return binsearch(arr, mid + 1, right, target);
        }
        return -1;

    }
    public static int binSearch(int srcArray[], int start, int end, int key) {
//        int mid = (end - start) / 2 + start;
        int mid = (end - start) >>> 1;
        if (srcArray[mid] == key) {
            return mid;
        }
        if (start >= end) {
            return -1;
        } else if (key > srcArray[mid]) {
            return binSearch(srcArray, mid + 1, end, key);
        } else if (key < srcArray[mid]) {
            return binSearch(srcArray, start, mid - 1, key);
        }
        return -1;
    }
    public static int binarySearchshangguigu(int[] arr, int target) {

        int left = 0;
        int right = arr.length - 1;
        while(left <= right) { //说明继续查找
            int mid = (left + right) / 2;
            if(arr[mid] == target) {
                return mid;
            } else if ( arr[mid] > target) {
                right = mid - 1;//需要向左边查找
            } else {
                left = mid + 1; //需要向右边查找
            }
        }
        return -1;
    }

    public static int binarySearchcsdn(int[] srcArray, int des) {
        //定义初始最小、最大索引
        int low = 0;
        int high = srcArray.length - 1;
        //确保不会出现重复查找，越界
        while (low <= high) {
            //计算出中间索引值
            int middle = (high + low) >>> 1;//防止溢出
            if (des == srcArray[middle]) {
                return middle;
                //判断下限
            } else if (des < srcArray[middle]) {
                high = middle - 1;
                //判断上限
            } else {
                low = middle + 1;
            }
        }
        //若没有，则返回-1
        return -1;
    }

}
